import java.util.HashMap;

public class varBlock {
    public HashMap<String, String> vars;
    public HashMap<String, String> constVars;
    public HashMap<String, String> allVars;
    public HashMap<String, Integer> constVars_value;//记录常量的常量值
    public HashMap<String, arrayIdent> varArrays;
    public HashMap<String, arrayIdent> constArrays;
    public HashMap<String, arrayIdent> allArrays;
    public varBlock outerBlock;

    public varBlock() {
        vars = new HashMap<>();
        constVars = new HashMap<>();
        allVars = new HashMap<>();
        constVars_value = new HashMap<>();
        varArrays = new HashMap<>();
        constArrays = new HashMap<>();
        allArrays = new HashMap<>();
        outerBlock = null;
    }

    public void add_vars(String ident, String var) {
        if (allVars.containsKey(ident)) {
            err("add_vars:变量在同一代码块中重复命名");
        }
        vars.put(ident, var);
        allVars.put(ident, var);
    }

    public void add_constVars(String ident, String constVar) {
        if (allVars.containsKey(ident)) {
            err("add_constVars:变量在同一代码块中重复命名");
        }
        constVars.put(ident, constVar);
        allVars.put(ident, constVar);
    }

    public void add_varArrays(String ident, arrayIdent array) {
        if (allArrays.containsKey(ident)) {
            err("add_varArrays:数组在同一代码块中重复命名");
        }
        varArrays.put(ident, array);
        allArrays.put(ident, array);
    }

    public void add_constArrays(String ident, arrayIdent array) {
        if (allArrays.containsKey(ident)) {
            err("add_constArrays:数组在同一代码块中重复命名");
        }
        constArrays.put(ident, array);
        allArrays.put(ident, array);
    }

    public arrayIdent find_varArray(String ident) {
        if (varArrays.containsKey(ident))
            return varArrays.get(ident);
        else {
            if (outerBlock == null) {
                err("find_varArray:未找到此数组");
                return null;
            } else
                return outerBlock.find_varArray(ident);
        }
    }

    public arrayIdent find_constArray(String ident) {
        if (constArrays.containsKey(ident))
            return constArrays.get(ident);
        else {
            if (outerBlock == null) {
                err("find_constArray:未找到此数组");
                return null;
            } else
                return outerBlock.find_constArray(ident);
        }
    }

    public arrayIdent find_allArray(String ident) {
        if (allArrays.containsKey(ident))
            return allArrays.get(ident);
        else {
            if (outerBlock == null) {
                err("find_allArray:未找到此数组");
                return null;
            } else
                return outerBlock.find_allArray(ident);
        }
    }

    //从最小局部寻找变量,没有返回null
    public String find_var(String ident) {
        if (vars.containsKey(ident)) {
            return vars.get(ident);
        } else {
            if (outerBlock == null) {
                err("find_var:未找到此变量");
                return null;
            } else
                return outerBlock.find_var(ident);
        }
    }

    public String find_allVar(String ident) {
        if (allVars.containsKey(ident)) {
            return allVars.get(ident);
        } else {
            if (outerBlock == null) {
                err("find_allVar:未找到此变量或常量");
                return null;
            } else
                return outerBlock.find_allVar(ident);
        }
    }

    public String find_constVar(String ident) {
        if (constVars.containsKey(ident)) {
            return constVars.get(ident);
        } else {
            if (outerBlock == null) {
                err("find_constVar:使用了未定义的常量");
                return null;
            } else
                return outerBlock.find_constVar(ident);

        }
    }

    public boolean constVar_contain(String ident) {
        if (constVars.containsKey(ident)) {
            return true;
        } else {
            if (outerBlock == null) {
                return false;
            } else
                return outerBlock.constVar_contain(ident);
        }
    }

    public int get_constVar_value(String ident) {
        if (constVars_value.containsKey(ident)) {
            return constVars_value.get(ident);
        } else {
            if (outerBlock == null) {
                err("get_constVar_value:使用了未定义的常量");
                return 0;
            } else
                return outerBlock.get_constVar_value(ident);
        }
    }

    public void add_constVar_value(String ident, int value) {
        constVars_value.put(ident, value);
    }

    // 报错信息
    public void err(String message) {
        System.out.println(message);
        System.exit(1);
    }
}
